﻿unit MainWindow;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons, OpenGL, Math, ActnList,
  StdActns, ToolWin, ActnMan, ActnCtrls, Ribbon, PlatformDefaultStyleActnCtrls,
  RibbonLunaStyleActnCtrls, ActnMenus, RibbonActnMenus, ImgList, ScreenTips,
  ComCtrls, OleCtrls, SHDocVw, ActnColorMaps, RibbonObsidianStyleActnCtrls,
  RibbonSilverStyleActnCtrls, ImportSTL, ToolPath, vtGLPanel;

{============================================================================={}

{============================================================================={}
type
  TForm2 = class(TForm)
    ActionManager1: TActionManager;
    ClearMemo: TAction;
    ColorDialog1: TColorSelect;
    FileExit1: TFileExit;
    FileOpen1: TFileOpen;
    FilePrintSetup1: TFilePrintSetup;
    FileSaveAs1: TFileSaveAs;
    FindDialog1: TFindDialog;
    FindMemo1: TAction;
    FontDialog1: TFontEdit;
    galery_Luna: TAction;
    galery_Obsidian: TAction;
    galery_Silver: TAction;
    Help1: TAction;
    Help2: TAction;
    ImageList1: TImageList;
    ImageList2: TImageList;
    ImportButton1: TAction;
    ImportDialog1: TOpenDialog;
    Memo1: TMemo;
    Panel1: TPanel;
    RadioOption1: TAction;
    RadioOption2: TAction;
    RadioOption3: TAction;
    Ribbon1: TRibbon;
    RibbonApplicationMenuBar1: TRibbonApplicationMenuBar;
    RibbonGroup1: TRibbonGroup;
    RibbonGroup2: TRibbonGroup;
    RibbonGroup3: TRibbonGroup;
    RibbonGroup4: TRibbonGroup;
    RibbonGroup5: TRibbonGroup;
    RibbonGroup6: TRibbonGroup;
    RibbonPage1: TRibbonPage;
    RibbonPage2: TRibbonPage;
    RibbonPage3: TRibbonPage;
    ScreenTipsManager1: TScreenTipsManager;
    ScreenTipsPopup1: TScreenTipsPopup;
    Splitter1: TSplitter;
    Start: TAction;
    StatusBar1: TStatusBar;
    Stop: TAction;
    StyleList1: TActionList;
    Styles: TAction;
    vtGLPanel1: TvtGLPanel;
    XPColorMap1: TXPColorMap;
    procedure ClearMemoExecute(Sender: TObject);
    procedure ColorDialog1Accept(Sender: TObject);
    procedure FileOpen1Accept(Sender: TObject);
    procedure FileSaveAs1Accept(Sender: TObject);
    procedure FindDialog1Find(Sender: TObject);
    procedure FindMemo1Execute(Sender: TObject);
    procedure FontDialog1Accept(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta:
        Integer; MousePos: TPoint; var Handled: Boolean);
    procedure galery_LunaExecute(Sender: TObject);
    procedure galery_ObsidianExecute(Sender: TObject);
    procedure galery_SilverExecute(Sender: TObject);
    procedure Help1Execute(Sender: TObject);
    procedure Help2Execute(Sender: TObject);
    procedure ImportButton1Execute(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
    procedure Memo1Click(Sender: TObject);
    procedure Memo1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure RadioOption1Execute(Sender: TObject);
    procedure RadioOption2Execute(Sender: TObject);
    procedure RadioOption3Execute(Sender: TObject);
    procedure StylesExecute(Sender: TObject);
    procedure vtGLPanel1Paint(Sender: TObject);
  private
    fFintTextPos: Integer;
    fTP: TToolPath;
    procedure ChangeSelected;
  end;

var
  Form2: TForm2;
  StartCycle : integer;

implementation


{$R *.dfm}

{
************************************ TForm2 ************************************
}
procedure TForm2.FormCreate(Sender: TObject);
begin
  DecimalSeparator := '.';
  vtGLPanel1.EnterGL;
  S := TSolid.Create;
  fTP := TToolPath.Create;
  TIntersectionPoints.Create;
  Memo1Change(Self);
  Form2.RadioOption1Execute(Self);
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  S.Free;
  fTP.Destroy;
end;

procedure TForm2.ChangeSelected;
var
  y: integer;
begin
  y:= Memo1.CaretPos.Y;
  fTP.SelectedBlock := y;
  vtGLPanel1.Repaint;
  StatusBar1.Panels[0].Text:= '   Выделена строка '+IntToStr(y+1);
end;

procedure TForm2.ClearMemoExecute(Sender: TObject);
begin
  S.Destroy;
  ftp.Destroy;
  S:= TSolid.Create;
  fTP := TToolPath.Create;
  Memo1.Clear;
  vtGLPanel1.Repaint;
end;

procedure TForm2.Memo1Change(Sender: TObject);
begin
  fTP.Clear;
  fTP.Load(Memo1.Lines);
  vtGLPanel1.Repaint;
end;

procedure TForm2.Memo1Click(Sender: TObject);
begin
  ChangeSelected;
end;

procedure TForm2.Memo1KeyDown(Sender: TObject; var Key: Word; Shift:
    TShiftState);
begin
  ChangeSelected;
end;

procedure TForm2.ImportButton1Execute(Sender: TObject);
begin
  if ImportDialog1.Execute then
    S.Load(ImportDialog1.FileName);
end;

procedure TForm2.vtGLPanel1Paint(Sender: TObject);
begin
  S.Draw;
  fTP.Paint;
end;
{==============================================================================}
procedure TForm2.ColorDialog1Accept(Sender: TObject);
begin
  Memo1.Color := ColorDialog1.Dialog.Color;
end;
{==============================================================================}
procedure TForm2.FileOpen1Accept(Sender: TObject);
begin
  Memo1.Lines.Clear;
  Memo1.Lines.LoadFromFile(FileOpen1.Dialog.FileName);
end;

procedure TForm2.FileSaveAs1Accept(Sender: TObject);
begin
  Memo1.Lines.SaveToFile(FileSaveAs1.Dialog.FileName);
end;

procedure TForm2.FindDialog1Find(Sender: TObject);
var
  S : string;
  StartPos : integer;
begin
  with TFindDialog(Sender) do
    begin
      if fFintTextPos = 0 then
        Options := Options - [frFindNext];

      if frFindNext in Options then
        begin
          StartPos := fFintTextPos + Length(FindText);
          S := Copy(Memo1.Lines.Text, StartPos, MaxInt);
        end
      else
        begin
          S := Memo1.Lines.Text;
          StartPos := 1;
        end;

     fFintTextPos := Pos(FindText, S);
     if fFintTextPos > 0 then
       begin
         fFintTextPos := fFintTextPos + StartPos - 1;
         Memo1.SelStart := fFintTextPos - 1;
         Memo1.SelLength := Length(FindText);
         Memo1.SetFocus;
       end
     else
       begin
         if frFindNext in Options then
           S := Concat('Поиск элемента "', FindText,'" в управляющей программе закончен.')
         else
           S := Concat('Элемент "', FindText, '" в управляющей программе не найден.');
         MessageDlg(S, mtError, [mbOK], 0);
       end;
    end;
end;
{==============================================================================}
procedure TForm2.FindMemo1Execute(Sender: TObject);
begin
   fFintTextPos := 0;
   FindDialog1.Execute;
end;

procedure TForm2.FontDialog1Accept(Sender: TObject);
begin
  Memo1.Font.Assign(FontDialog1.Dialog.Font);
end;

procedure TForm2.FormKeyPress(Sender: TObject; var Key: Char);
begin
  ChangeSelected;
end;

procedure TForm2.FormMouseWheel(Sender: TObject; Shift: TShiftState;
    WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
  WheelDelta:= -WheelDelta;
  vtGLPanel1.DoMouseWheel(Shift,WheelDelta,MousePos);
  if (MousePos.X <= Memo1.Width)  then
    SendMessage(Memo1.Handle,EM_LINESCROLL,0,WheelDelta);
  Handled:= True;
end;

{==============================================================================}
// Оформление
procedure TForm2.StylesExecute(Sender: TObject);
begin
  Ribbon1.Style := RibbonLunaStyle;
end;
procedure TForm2.galery_LunaExecute(Sender: TObject);
begin
  Ribbon1.Style := RibbonLunaStyle;
end;

procedure TForm2.galery_ObsidianExecute(Sender: TObject);
begin
  Ribbon1.Style := RibbonObsidianStyle;
end;

procedure TForm2.galery_SilverExecute(Sender: TObject);
begin
  Ribbon1.Style := RibbonSilverStyle
end;

procedure TForm2.RadioOption1Execute(Sender: TObject);
begin
  RadioOption1.Checked:= True;
  XPColorMap1.FontColor:= clBlack;
  XPColorMap1.HotFontColor:= clWhite;
  Ribbon1.ColorMap:= XPColorMap1;
  Ribbon1.Repaint;
end;

procedure TForm2.RadioOption2Execute(Sender: TObject);
begin
  RadioOption2.Checked:= True;
  XPColorMap1.FontColor:= clBlue;
  XPColorMap1.HotFontColor:= clGradientActiveCaption;
  Ribbon1.ColorMap:= XPColorMap1;
  Ribbon1.Repaint;
end;

procedure TForm2.RadioOption3Execute(Sender: TObject);
begin
  RadioOption3.Checked:= True;
  XPColorMap1.FontColor:= clMaroon;
  XPColorMap1.HotFontColor:= clRed;
  Ribbon1.ColorMap:= XPColorMap1;
  Ribbon1.Repaint;
end;
{==============================================================================}
// Справка
procedure TForm2.Help1Execute(Sender: TObject);
begin
  MessageDlg('Программа «NCViewer» находится на стадии разработки',mtInformation,[mbOK],1);
end;

procedure TForm2.Help2Execute(Sender: TObject);
begin
  MessageDlg('Пока пишет только Грошев Александр Владимирович.'+#13#10+'Смотрят: Гимадиев М.Н.'#13#10'                 Сахапов С.М.'#13#10'                 Султанов Р.Р.',mtInformation,[mbOK],1);
end;
{==============================================================================}



{============================================================================={}
end.

